package cn.xhh.dailyreview.server.model;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.apache.commons.lang.StringUtils;

import cn.xhh.dailyreview.server.utils.Utility;

/**
 * The model class: Category<br/>
 * It is a one-to-many relation between Item and Review.<br/>
 * Each user can define his own items (User-to-Category: one-to-many).
 * @author cooky
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Category {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	
	@Persistent
	private String name;
	
	@Persistent
	private Long userId;

	public Category(String name) {
		this.name = name;
	}
	
	public Category(User user, String name) {
		this.name = name;
		userId = user.getId();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}
	
	public Long getUserId() {
		return userId;
	}

	public Long getId() {
		return id;
	}

	@Override
	public String toString() {
		return name;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		
		if (obj==null || (obj.getClass() != this.getClass()))
			return false;
		
		Category c = (Category) obj;
		return Utility.isEqual(name, c.getName());
	}
	
	@Override
	public int hashCode() {
		int hash = 7;
		hash = 31 * hash + (null == name ? 0 : name.hashCode());
		return hash;
	}

	public boolean valid() {
		return userId!=null && userId>0 && StringUtils.isNotBlank(name);
	}
	
	public boolean invalid() {
		return !valid();
	}

}
